app.controller('adminDataCtrl', ['$scope', function($scope) {
	var needNewPagination = true;
	var param = null;
	
	//获取管理员基本信息
	AQ.callAQJSON("getAdminInfo.do", null, function(result) {
		var list = result.data;
		$scope.$apply(function() {
			$scope.AdminInfo = list[0];
		});
	});
	
	//查看用户
	$("#search-user").click(function() {
		$(".left-bar").children("li").css("background", "#FFF");
		$("#search-user").css("background", "#DDF3FD");
		$("#now-loca-txt").text("查看用户信息");
		$(".search").show();		
	});
	
	//在线用户
	$("#online-users").click(function() {
		$(".left-bar").children("li").css("background", "#FFF");
		$("#online-users").css("background", "#DDF3FD");
		$("#now-loca-txt").text("当前在线用户");
		$(".search").hide();
		
		param = {
			userKey: null,
			tableName: "onlineUsers",
			pageNo: 1,
			pageCount: 10,
		};
		queryData(param, 1);
    });
	
	//本站注册用户
	$("#site-users").click(function() {
		$(".left-bar").children("li").css("background", "#FFF");
		$("#site-users").css("background", "#DDF3FD");
		$("#now-loca-txt").text("本站注册用户");
		$(".search").hide();
		
		param = {
			userKey: null,
			tableName: "siteUsers",
			pageNo: 1,
			pageCount: 10,
		};
		queryData(param, 1);
    });
	
	//账户明细记录
	$("#site-money").click(function() {
		$(".left-bar").children("li").css("background", "#FFF");
		$("#site-money").css("background", "#DDF3FD");
		$("#now-loca-txt").text("账户明细记录");
		$(".search").hide();
		
		param = {
			userKey: null,
			tableName: "siteMoney",
			pageNo: 1,
			pageCount: 10,
		};
		queryData(param, 1);
    });
	
	//账户余额提现
	$("#tixian").click(function() {
		var content = '<div style="color:#B5B4B2; padding-bottom:10px;">● 提现是把您的管理账户余额转到对应的飞钻账户；<BR>● 单次提现金额不得小于10元；</div>' + 
		'提现金额：<input type="text" id="tx-money" style="width:210px; height:35px; border:1px solid #B5B4B2;"/>';
		Utils.checkDialog(content, "&nbsp;确定转出&nbsp;", "返回", function() {
			var money = $.trim($("#tx-money").val());
			var param = {
				money: money,
			};
			AQ.callAQJSON("adminTX.do", param, function(result) {
				Utils.msgDialog(result.info);
			}, function(err, code) {
				Utils.msgDialog(err + "["+ code +"]");
			});
		}, function() {});
	});
	
	//查看本站信息
	$("#set-site").click(function() {
		AQ.callAQJSON("getAdminInfo.do", null, function(result) {
			var list = result.data;
			var AdminInfo = list[0];
			
			var content = '<table border="0" cellpadding="15" cellspacing="0" style="color:#275d8b; font-size:14px; font-weight:bold;">'+
			'<tr><td>分站编号：'+AdminInfo.siteNo+'</td><td>账户余额：'+AdminInfo.money+'</td><td>飞钻账户：'+AdminInfo.userName+'</td></tr>'+
			'<tr><td>管理姓名：'+AdminInfo.realName+'</td><td>手机号：'+AdminInfo.phone+'</td><td>邮箱：'+AdminInfo.mail+'</td></tr>'+
			'<tr><td>创建时间：'+AdminInfo.createTime+'</td><td>客服QQ：'+AdminInfo.siteQQ+'</td><td>交流群：'+AdminInfo.siteQQqun+'</td></tr>'+
			'</table>';
			
		var d = dialog({
				fixed: true,
				zIndex: 999999999,
	    		title: '查看本站信息',
	  	  		content: content,
				okValue: '返回',
	    		ok: function () {}
		});
		d.width(650);
		d.height(200);
		d.showModal();
			
			$scope.$apply(function() {
				$scope.AdminInfo = AdminInfo;
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	});
	
	
	//点击查看用户
	$("#query-table").click(function() {
		var keyType = $("input[name='keyType']:checked").val();
		var userKey = keyType + $.trim($("#user-key").val());
		var tableName = $("#table-name").val();
		
		var qqReg = /^[0-9]{5,11}$/;
		if(keyType == "qq" && !qqReg.test($.trim($("#user-key").val()))) {
			Utils.msgDialog("QQ号格式不正确！");
			return false;
		}
		
		param = {
			userKey: userKey,
			tableName: tableName,
			pageNo: 1,
			pageCount: 10,
		};
		queryData(param, 1);
    });
	
	
	Utils.newPagination("Pagination", 1000, function(page_index, jq) {
		if(page_index == 0 && needNewPagination) {
			needNewPagination = false;
			return;
		}
		queryData(param, page_index+1);
	});
	
	
	/*
	查询数据
	*/
	function queryData(param, pageNo) {
		$("#data-table tr").each(function(index, element) {
			$(element).remove();	//清空行
		});
		$("#Pagination").hide();
		$(".obj-tb").hide();
		
		param.pageNo = pageNo;
		AQ.callAQJSON("queryTable.do", param, function(result) {
			var list = result.data;
			var column = result.info;	//列数
			
			//单行对象记录：User
			if(param.tableName == "user") {
				$(".obj-tb").show();
				var item = {};
				for(var n = 0; n < column; n++) {
					var propName = "C"+n;
					var key = list[0][propName];
					var value = list[1][propName];
					if(typeof(value) == "undefined" || value == "null") {
						value = "";
					}
					
					if(key == "identity_level") {
						value = value == 0 ? "未认证" : "已认证";
					}
					if(key == "sex") {
						value = value == 1 ? "男" : "女";	
					}
					
					item[key] = value;
				}
				
				$scope.$apply(function() {
					$scope.item = item;
				});
				return;
			}
			
			//多行记录
			$("#Pagination").show();
			var data = [];
			for (var i = 0; i < list.length; i++) {
				//console.log(JSON.stringify(list[i]));
				var tds = "";
				for(var n = 0; n < column; n++) {
					var propName = "C"+n;
					var value = list[i][propName];
					if(typeof(value) == "undefined" || value == "null") {
						value = "";
					}
					if(i == 0) {	//第一行：列明
						var columnName = ExplainColumn[value];
						columnName = typeof(columnName) == "undefined" ? value : columnName;
						tds += '<td>'+ columnName +'</td>';
					} else {
						tds += '<td><input type="text" readOnly="true" value="'+ value +'"/></td>';
					}
				}
				if(i == 0) {	//第一行：列明
					$("#data-table").append('<tr class="tb-head">'+ tds +'</tr>');
				} else {
					$("#data-table").append('<tr>'+ tds +'</tr>');
				}
			}
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	/**
	点击QQ
	*/
	$scope.qqTalk = function(qq) {
		location.replace("tencent://message/?uin="+ qq +"&Site=qq&Menu=yes");
	};

}]);


/**
 * 列明解释
 */
var ExplainColumn = {
	id : "ID",
	user_id : "用户ID",
	user_name : "用户名",
	
	update_time : "更新时间",
	update_type : "类别",
	update_money : "交易金额",
	money : "余额",
	info : "信息",
	operator : "操作人",
	
	update_feibi : "飞币交易量",
	feibi : "飞币",
	
	real_name : "姓名",
	apply_time : "申请时间",
	state : "状态",
	zz_account : "转账账户",
	tx_account : "提现账户",
	deal_time : "处理时间",
	
	login_time : "登录时间",
	region : "省份",
	city : "市",
	county : "县/区",
	isp : "运营商",
	user_agent : "浏览器信息",
	
	trade_no : "交易号",
	trade_money : "金额",
	trade_info : "信息",
	trade_time : "时间",
	is_used : "是否使用",
	use_time : "使用时间",
	insert_time : "创建时间",
	
	buyer_id : "买号ID",
	buyer_nick : "买号名",
	is_real_name : "是否实名认证",
	buyer_rank : "买号等级",
	bind_time : "绑定时间",
	day_take : "日接单量",
	week_take : "周接单量",
	month_take : "月接单量",
	note : "备注",
	
	seller_id : "掌柜号ID",
	seller_nick : "掌柜名",
	is_verify : "是否验证",
	seller_rank : "等级",
	day_publish : "日放单量",
	week_publish : "周放单量",
	month_publish : "月放单量",
	
	task_id : "任务编号",
	is_cancel : "是否取消",
	refund_type : "付款/返款方式",
	total_money : "总金额",
	publish_time : "发布时间",
	show_time : "显示时间",
	take_time : "接单时间",
	accept_time : "审核时间",
	pay_time : "付款时间",
	over_time : "到时时间",
	confirm_receive_time : "收货时间",
	finish_time : "完成时间",
	check_link : "验证链接",
	publisher_id : "放单人ID",
	taker_id : "接单人ID",
}
